package com.bell.atensettings.settingDetails;

import java.text.Collator;
import java.util.Locale;

import com.bell.atensettings.R;

import android.app.Activity;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.app.backup.BackupManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LocaleSettings extends Activity implements OnItemClickListener
{
    private ImageView bridgeImage;
    private int index = 0;
    private ItemAdapter itemAdapter;
    private TextView mTitleView;
    private ListView lv;
    LocaleSelectionListener mListener;

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }

    @Override
    protected void onCreate(Bundle saveState)
    {
        super.onCreate(saveState);
        setContentView(R.layout.public_settings);
        
        mTitleView = (TextView) findViewById(R.id.setting_title);
        lv = (ListView) findViewById(R.id.listview);
        
        mTitleView.setText(getIntent().getStringExtra("title"));
        initListItem();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        lv.requestFocus();
    }

    public static interface LocaleSelectionListener
    {
        public abstract void onLocaleSelected(Locale locale);
    }

    public LocaleSettings()
    {
        index = 0;
    }

    private static String getDisplayName(Locale locale, String[] locales, String[] disps)
    {
        int index = 0;
        for (index = 0; index < locales.length; index++)
        {
            if (locales[index].equals(locale.toString()))
                return disps[index];
        }

        return locale.getDisplayName(locale);
    }

    private void initListItem()
    {
        initItem();
    }

    private void initItem()
    {
        LocaleInfo localeInfo[] = getLocalInfo(this);
        index = findLanguage(localeInfo);
        itemAdapter = new ItemAdapter(this, localeInfo);
        
        lv.setAdapter(itemAdapter);
        lv.setOnItemClickListener(this);
    }

    private String toTitleCase(String title)
    {
        if (title.length() > 0)
        {
            return Character.toUpperCase(title.charAt(0)) + title.substring(1);
        }
        return title;
    }

    private LocaleInfo[] getLocalInfo(Context context)
    {
        Resources resources = context.getResources();
        String locales[] = resources.getStringArray(R.array.special_locale_codes);
        String localeName[] = resources.getStringArray(R.array.special_locale_names);
        LocaleInfo localeInfos[] = new LocaleInfo[locales.length];
        int k = 0;
        
        for (int index = 0; index < locales.length; index++)
        {
            String locale_code = locales[index];
            LocaleInfo alocaleinfo1[];
            int l;
            int i1;
            if (locale_code.length() == 5)
            {
                String s1 = locale_code.substring(0, 2);
                Locale locale = new Locale(s1, locale_code.substring(3, 5));
                if (k == 0)
                {
                    Log.v("daming.zou**",
                            (new StringBuilder()).append("adding initial ")
                                    .append(toTitleCase(locale.getDisplayLanguage(locale))).toString());
                    i1 = k + 1;
                    LocaleInfo localeinfo2 = new LocaleInfo(toTitleCase(locale.getDisplayLanguage(locale)), locale);
                    localeInfos[k] = localeinfo2;
                }
                else if (localeInfos[k - 1].locale.getLanguage().equals(s1))
                {
                    Log.v("daming.zou***",
                            (new StringBuilder()).append("backing up and fixing ").append(localeInfos[k - 1].label)
                                    .append(" to ")
                                    .append(getDisplayName(localeInfos[k - 1].locale, locales, localeName)).toString());
                    localeInfos[k - 1].label = toTitleCase(getDisplayName(localeInfos[k - 1].locale, locales,
                            localeName));
                    Log.v("daming.zou***",
                            (new StringBuilder()).append("  and adding ")
                                    .append(toTitleCase(getDisplayName(locale, locales, localeName))).toString());
                    i1 = k + 1;
                    LocaleInfo localeinfo1 = new LocaleInfo(toTitleCase(getDisplayName(locale, locales, localeName)),
                            locale);
                    localeInfos[k] = localeinfo1;
                }
                else
                {
                    String s2;
                    LocaleInfo localeinfo;
                    if (locale_code.equals("zz_ZZ"))
                        s2 = "Pseudo...";
                    else
                        s2 = toTitleCase(locale.getDisplayLanguage(locale));
                    Log.v("daming.zou***", (new StringBuilder()).append("adding ").append(s2).toString());
                    i1 = k + 1;
                    localeinfo = new LocaleInfo(s2, locale);
                    localeInfos[k] = localeinfo;
                }
            }
            else
            {
                i1 = k;
            }
            k = i1;
        }
        LocaleInfo[] alocaleinfo1 = new LocaleInfo[k];
        for (int l = 0; l < k; l++)
            alocaleinfo1[l] = localeInfos[l];

        return alocaleinfo1;
    }
    
    private void updateLocale(Locale locale)
    {
        IActivityManager iActivityManager = ActivityManagerNative.getDefault();
        try
        {
            Configuration config = iActivityManager.getConfiguration();
            config.locale = locale;
            config.userSetLocale = true;
            
            iActivityManager.updateConfiguration(config);
            BackupManager.dataChanged("com.android.providers.settings");
        }
        catch (RemoteException exception)
        {
            exception.printStackTrace();
        }
    }

    private int findLanguage(LocaleInfo localInfo[])
    {
        IActivityManager activityManager = ActivityManagerNative.getDefault();
        try
        {
            Configuration config = activityManager.getConfiguration();

            for (int index = 0; index < localInfo.length; index++)
            {
                if (config.locale.equals(localInfo[index].locale))
                    return index;
            }

        }
        catch (RemoteException exception)
        {
            exception.printStackTrace();
        }
        return 0;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterview, View view, int i, long l)
    {
        if (bridgeImage != null)
            bridgeImage.setImageResource(R.drawable.unright_icon);
        ImageView imageView = (ImageView) view.getTag();
        imageView.setImageResource(R.drawable.right_icon);
        bridgeImage = imageView;
        Locale locale = ((LocaleInfo) lv.getAdapter().getItem(i)).locale;
        
        updateLocale(locale);
    }

    public static class LocaleInfo implements Comparable
    {
        static final Collator sCollator = Collator.getInstance();
        String label;
        Locale locale;

        public LocaleInfo(String label1, Locale locale1)
        {
            label = label1;
            locale = locale1;
        }

        public int compareTo(Object obj)
        {
            return compareTo((LocaleInfo) obj);
        }

        public int compareTo(LocaleInfo localeinfo)
        {
            return sCollator.compare(label, localeinfo.label);
        }

        public String getLabel()
        {
            return label;
        }

        public Locale getLocale()
        {
            return locale;
        }

        public String toString()
        {
            return label;
        }
    }

    public class ItemAdapter extends BaseAdapter
    {
        private LocaleInfo mItem[];

        public ItemAdapter(Context context, LocaleInfo alocaleinfo[])
        {
            super();
            mItem = alocaleinfo;
        }

        public int getCount()
        {
            return mItem.length;
        }

        public Object getItem(int i)
        {
            return mItem[i];
        }

        public long getItemId(int i)
        {
            return (long) i;
        }

        public View getView(int position, View view, ViewGroup viewgroup)
        {
            LocaleInfo localeinfo = mItem[position];
            if (view == null)
                view = ((LayoutInflater) getSystemService("layout_inflater")).inflate(
                        R.layout.list_display_item, null);
            view.setBackgroundResource(R.drawable.listnormal);
            ImageView imageview = (ImageView) view.findViewById(R.id.img_sign);
            TextView textview = (TextView) view.findViewById(R.id.tv_data);
            if (position == index)
            {
                bridgeImage = imageview;
                imageview.setImageResource(R.drawable.right_icon);
            }
            else
            {
                imageview.setImageResource(R.drawable.unright_icon);
            }
            textview.setText(localeinfo.toString());
            view.setTag(imageview);
            return view;
        }

    }

}
